/*
setup GUI
*/

function generateMySetupBoxOnOptionsPage()	{
	
	var i = 0;
	var actualRow = null;
	var actualThCell = null;
	
	var emailChangeDiv = $X("//*[@id='mainview']/div/div/form/input[@value='changeEmail']/../../.." );	
	
	var setupContainer = document.createElement('div');
	setupContainer.id = 'setupContainer';	
	setupContainer.className = 'contentBox01h';
	
	var headerObj = document.createElement('h3');
	headerObj.className = 'header';
	
	var headerSpanObj = document.createElement('span');
	headerSpanObj.className = '';
	headerSpanObj.appendChild ( document.createTextNode( getOtherTextInLang('SETUP_BOX_HEADER') + ' ' + getLanguageForDisplay() ) );
	headerObj.appendChild( headerSpanObj );	
	setupContainer.appendChild( headerObj );
	
	var contentDiv = document.createElement('div');
	contentDiv.id = 'contentDiv';	
	contentDiv.className = 'content';
	
	contentDiv.appendChild( createSetupTable() );
	
	var saveButton = createInputElement('button', 'MessageManagerSaveButton', getButtonTextInLang('SAVE_MESSAGE_MANAGER_SETTINGS_BUTTON_TEXT'), 'button' );
	addEvent( saveButton, 'click', function(){saveUserChanges()}, true );		
	addEvent( saveButton, 'click', function(){history.back()}, true );
	
	var saveButtonDiv = document.createElement('div');
	saveButtonDiv.className = 'centerButton';
	
	saveButtonDiv.appendChild( saveButton );
			
	contentDiv.appendChild( saveButtonDiv );	
	setupContainer.appendChild( contentDiv );	
	
	// User defined keywords
	
	var userDefinedKeywordDiv = document.createElement('div');
	userDefinedKeywordDiv.id = 'UserDefinedKeywordDiv';	
	userDefinedKeywordDiv.className = 'content';
	
	var userDefinedKeywordHeader = document.createElement('h3');
	userDefinedKeywordHeader.id = 'UserDefinedKeywordHeader';
	userDefinedKeywordHeader.appendChild( document.createTextNode( getSetupTextInLang( 'USER_DEFINED_KEYWORD_MANAGEMENT_TEXT' ) ) );
	
	userDefinedKeywordDiv.appendChild( userDefinedKeywordHeader );
	
	var userDefinedKeywordTable = document.createElement('table');
	userDefinedKeywordTable.id = 'UserDefinedKeywordTable';
	userDefinedKeywordTable.width = '90%';	
	
	userDefinedKeywordTable.setAttribute('cellpadding', '0');
	userDefinedKeywordTable.setAttribute('cellspacing', '0');
	userDefinedKeywordTable.setAttribute('border', '1');
	
	var userDefinedKeywordTableBody = document.createElement('tbody');	
	var userDefinedKeywordTableHeaderTexts = getTableHeadersDataInLang( 'USER_DEFINED_KEYWORDS_TABLE' );
	actualRow = document.createElement('tr');
	for ( i = 0; i != userDefinedKeywordTableHeaderTexts.length; i++  )	{
		actualThCell = document.createElement('th');
		actualThCell.appendChild( document.createTextNode( userDefinedKeywordTableHeaderTexts[i] ) );
		actualRow.appendChild( actualThCell );
	}	
	userDefinedKeywordTableBody.appendChild( actualRow );
	
	// get data
	//	s4.ikariam.hu##{keyword1=option1;picture1path||keyword2=option2;picture2path}	
	var keyName = null;
	var i = 0;
	for ( keyName in userDefinedMenuOptions )	{		
		actualRow = createRowForUserDefinedKeywords( keyName, i );
		userDefinedKeywordTableBody.appendChild( actualRow );
		i++;
	}
	
	userDefinedKeywordTable.appendChild( userDefinedKeywordTableBody );	
	userDefinedKeywordDiv.appendChild( userDefinedKeywordTable );
	
	var userDefinedKeywordButtonsTable = document.createElement('table');
	userDefinedKeywordButtonsTable.id = 'userDefinedKeywordButtonsTable';
	userDefinedKeywordButtonsTable.width = '90%';
	userDefinedKeywordButtonsTable.setAttribute('cellpadding', '0');
	userDefinedKeywordButtonsTable.setAttribute('cellspacing', '0');
	
	var userDefinedKeywordButtonsTableBody = document.createElement('tbody');
	actualRow = document.createElement('tr');
	
	var addKeywordButtonCell = document.createElement('td');
	addKeywordButtonCell.id = 'addKeywordButtonCell';
	var addKeywordButton = createInputElement('button',  'addKeywordButton', getButtonTextInLang( 'ADD_NEW_USER_DEFINED_KEYWORD_TEXT' ), 'button' );
	addKeywordButton.style.margin = 'auto';
	addKeywordButton.style.padding = 'auto';	
	
	addEvent( addKeywordButton, 'click', function(){ addNewUserDefinedKeywordLine() }, true );	
	addKeywordButtonCell.appendChild( addKeywordButton );
	actualRow.appendChild( addKeywordButtonCell );	
	
	var saveKeywordButtonCell = document.createElement('td');
	saveKeywordButtonCell.id = 'saveKeywordButtonCell';
	var saveKeywordButton = createInputElement('button',  'saveKeywordButton', getButtonTextInLang( 'SAVE_SELECTED_USERDEFINED_KEYWORDS_TEXT' ), 'button' );
	saveKeywordButton.style.margin = 'auto';
	saveKeywordButton.style.padding = 'auto';	
	addEvent( saveKeywordButton, 'click', function(){ saveUserDefinedKeywords() }, true );	
	saveKeywordButtonCell.appendChild( saveKeywordButton );
	actualRow.appendChild( saveKeywordButtonCell );	
	
	var deleteKeywordButtonCell = document.createElement('td');
	deleteKeywordButtonCell.id = 'deleteKeywordButtonCell';
	var deleteKeywordButton = createInputElement('button',  'deleteKeywordButton', getButtonTextInLang( 'REMOVE_SELECTED_USERDEFINED_KEYWORDS_TEXT' ), 'button' );
	deleteKeywordButton.style.margin = 'auto';
	deleteKeywordButton.style.padding = 'auto';	
	addEvent( deleteKeywordButton, 'click', function(){ deleteUserDefinedKeyWords() }, true );	
	deleteKeywordButtonCell.appendChild( deleteKeywordButton );
	actualRow.appendChild( deleteKeywordButtonCell );
	
	userDefinedKeywordButtonsTableBody.appendChild( actualRow );
	userDefinedKeywordButtonsTable.appendChild( userDefinedKeywordButtonsTableBody );
	userDefinedKeywordDiv.appendChild( userDefinedKeywordButtonsTable );	
	
	setupContainer.appendChild( userDefinedKeywordDiv );
	
//	setupUserDefinedKeywordMenus();
//	release is suspended due to Ikariam v.0.3.1.	
	
	// Debug data
	
	var debugDiv = document.createElement('div');
	debugDiv.id = 'MessageManagerDebugDiv';	
	debugDiv.className = 'content';
	
	var debugHeader = document.createElement('h3');
	debugHeader.id = 'MessageManagerDebugHeader';
	debugHeader.appendChild( document.createTextNode( getSetupTextInLang( 'DEBUG_DATA_HEADER_TEXT' ) ) );
	
	debugDiv.appendChild( debugHeader );
	
	var debugTable = document.createElement('table');
	debugTable.id = 'MessageManagerDebugTable';
	debugTable.setAttribute('cellpadding', '0');
	debugTable.setAttribute('cellspacing', '0');
	
	var debugTableBody = document.createElement('tbody');
	var allyDetailsString = GM_getValue( 'PLAYER_ALLY_DETAILS' );
	var allyDetailArray = allyDetailsString == null || allyDetailsString.length == 0 ? new Array('N/A', 'N/A') : allyDetailsString.split('=');
	if ( allyDetailArray[0] == '0' )	{
		allyDetailArray[0] = 'N/A';
	}
	
	var allyNameRow = document.createElement('tr');
	var allyNameText = document.createElement('td');
	allyNameText.style.backgroundColor = '#FAEAC6';
	allyNameText.appendChild( document.createTextNode( getSetupTextInLang( 'USER_ALLY_NAME_TEXT' ) ) );
	var allyNameValue = document.createElement('td');
	allyNameValue.appendChild( document.createTextNode( allyDetailArray[1] ) );
	allyNameRow.appendChild( allyNameText );
	allyNameRow.appendChild( allyNameValue );
	
	debugTableBody.appendChild( allyNameRow );
	
	var allyIdRow = document.createElement('tr');
	var allyIdText = document.createElement('td');
	allyIdText.appendChild( document.createTextNode( getSetupTextInLang( 'USER_ALLY_ID_TEXT' ) ) );
	var allyIdValue = document.createElement('td');
	allyIdValue.style.backgroundColor = '#FAEAC6';
	allyIdValue.appendChild( document.createTextNode( allyDetailArray[0] ) );
	allyIdRow.appendChild( allyIdText );
	allyIdRow.appendChild( allyIdValue );
	
	debugTableBody.appendChild( allyIdRow );
	
	debugTable.appendChild( debugTableBody );	
	debugDiv.appendChild( debugTable );	
	
	var refreshAllyDetailsButton = createInputElement('button', 'MessageManagerRefreshAllyDetailsButton', getButtonTextInLang('REFRESH_ALLY_DETAILS_TEXT'), 'button' );
	addEvent( refreshAllyDetailsButton, 'click', function(){
		requestMyDetails( function(responseDetails){savePlayerAllyDetails(responseDetails.responseText)} )
	}, true );		
	addEvent( refreshAllyDetailsButton, 'click', function(){history.back()}, true );
	
	var refreshAllyDetailsButtonDiv = document.createElement('div');
	refreshAllyDetailsButtonDiv.className = 'centerButton';
	
	refreshAllyDetailsButtonDiv.appendChild( refreshAllyDetailsButton );
			
	debugDiv.appendChild( refreshAllyDetailsButtonDiv );	
	setupContainer.appendChild( debugDiv );
	
	// Addressbook management
	
	addressBookList = getAddressBookList();
	
	if ( addressBookList != 'undefined' && addressBookList != null )	{
		var addressBookDiv = document.createElement('div');
		addressBookDiv.id = 'MessageManagerAddressBookDiv';	
		addressBookDiv.className = 'content';
		
		var addressBookHeader = document.createElement('h3');
		addressBookHeader.id = 'MessageManagerAddressBookHeader';
		addressBookHeader.appendChild( document.createTextNode( getSetupTextInLang( 'ADDRESSBOOK_MANAGEMENT_TEXT' ) ) );
		
		addressBookDiv.appendChild( addressBookHeader );
		var addressBookTable = document.createElement('table');
		addressBookTable.id = 'MessageManagerAddressBookTable';
		addressBookTable.setAttribute('cellpadding', '0px');
		addressBookTable.setAttribute('cellspacing', '0px');
		
		var addressBookTableBody = document.createElement('tbody');		
		var addressBookArray = convertMapToArray( addressBookList );
		addressBookArray.sort( sortMultidimensionalArrayBySecondValInCaseSensitiveMode );
		
		for ( i = 0; i != addressBookArray.length; i++ )	{
			actualRow = document.createElement('tr');
			var playerId = addressBookArray[i][0];
			actualRow.id = 'opt_addr_book_' + playerId;
			
			var nameCell = document.createElement('td');
			nameCell.id = i;
			nameCell.appendChild( document.createTextNode( addressBookArray[i][1] ) );
			
			if ( i % 2 == 0 )	{
				nameCell.style.backgroundColor = '#FAEAC6';
			}
			
			actualRow.appendChild( nameCell );			
			
			var removeCell = document.createElement('td');
			removeCell.valign = 'middle';
			
			var removePicture = createSubjectPicture(REMOVE_USER_FROM_ADDRESSBOOK_ICON_PATH);
			removePicture.width = '20';
			removePicture.height = '20';
			removePicture.id = 'pic' + playerId;
			removeCell.appendChild( removePicture );
			removeCell.style.width = '25px';
			removeCell.title = getOtherTextInLang( 'REMOVE_USER_FROM_ADDRESSBOOK' );

			if ( i % 2 != 0 )	{
				removeCell.style.backgroundColor = '#FAEAC6';
			}			
			
			addEvent( removePicture, 'click', function( playerId ){ deleteAddressBookEntry( this.id ) }, true );			
			actualRow.appendChild( removeCell );			
			addressBookTableBody.appendChild( actualRow );			
		}
		
		
		addressBookTable.appendChild( addressBookTableBody );	
		addressBookDiv.appendChild( addressBookTable );	
		
		setupContainer.appendChild( addressBookDiv );
	}
	
	var footerDiv = document.createElement('div');
	footerDiv.className = 'footer';
	setupContainer.appendChild( footerDiv );	

	emailChangeDiv.parentNode.insertBefore( setupContainer, emailChangeDiv.nextSibling );	
}

function createSetupTable(){
	var setupTable = document.createElement('table');
	setupTable.id = 'setupTable';
	setupTable.setAttribute('cellpadding', '0');
	setupTable.setAttribute('cellspacing', '0');
	setupTable.style.margin = 'auto';
	
	var setupTableBody = document.createElement('tbody');
	var actualRow = null;
	var actualCell = null;
	var backgorungSelector = 0;
	for (keyName in userSettingElements) {
	
		actualRow = document.createElement('tr');		 
		
		actualCell = document.createElement('td');
		actualCell.appendChild( document.createTextNode( getSetupTextInLang(keyName + '_TEXT') ) );
		if ( backgorungSelector % 2 == 0 )	{
			actualCell.style.backgroundColor = '#FAEAC6';
		}
		
		actualRow.appendChild(actualCell);
		
		actualCell = document.createElement('td');
		actualCell.appendChild(createElementForSetupTable(keyName));
		if ( backgorungSelector % 2 == 1 )	{
			actualCell.style.backgroundColor = '#FAEAC6';
		}
		
		actualRow.appendChild(actualCell);
		
		setupTableBody.appendChild(actualRow);
		
		backgorungSelector++;
	}
	
	setupTable.appendChild(setupTableBody);
	
	return setupTable;
}

function createElementForSetupTable( userSettingElementName )	{
	var tagType = userSettingElements[userSettingElementName][0];	
	var elementValue = null;	
	
	var tagElement = createInputElement(tagType, userSettingElementName + '_ID', null, null);
	
	var tempValue = userSettingValues[userSettingElementName];
	if ( 'div' == userSettingElements[userSettingElementName][1] )	{
		elementValue = convertDivDisplayValueToBoolean( tempValue );
	}
	else if ( 'select' == userSettingElements[userSettingElementName][0] )	{
		elementValue = tempValue;
	}	
	else	{
		elementValue = tempValue;
	}
	
	if ( userSettingsDisplayAbleValues == 'undefined' || userSettingsDisplayAbleValues == null )	{		
		userSettingsDisplayAbleValues = new Object();
	}
	
	userSettingsDisplayAbleValues[tagElement.id] = elementValue;
		
	if ( 'checkbox' == tagType )	{
		tagElement.checked = elementValue;
	}
	else if ( 'select' == tagType )	{
		var selectedIdx = getValueIndexFromArray(convertSelectOptionsToArray(tagElement), elementValue);
		tagElement.selectedIndex = selectedIdx;
	}

	// TODO
	if ( userSettingElementName == 'UPDATE_USER_NAMES_IN_ADDRESSBOOK' )	{		
		tagElement.disabled=true;
	}
	
	return tagElement;
}

function saveUserChanges()	{	
	var tdElements = $x("//*[@id='mainview']/div/div/table[@id='setupTable']/tbody/tr/td/input");	
	
	for ( i = 0; i != tdElements.length; i++ )	{
		setSavedUserSettingValue(tdElements[i].id, tdElements[i].checked );
		var userSettingName = tdElements[i].id.replace(/_ID/, '');
		userSettingValues[userSettingName] =  'div' == userSettingElements[userSettingName][1] 
					? convertBooleanToDivDisplayValue( tdElements[i].checked  )
					: tdElements[i].checked ;		
		saveUserSettingValue(userSettingName, userSettingValues[userSettingName]);		
	}

	tdElements = $x("//*[@id='mainview']/div/div/table[@id='setupTable']/tbody/tr/td/select");
	
	for ( i = 0; i != tdElements.length; i++ )	{
		userSettingName = tdElements[i].id.replace(/_ID/, '');		
		userSettingValues[userSettingName] = 'int' == userSettingElements[userSettingName][1]
			? parseInt( tdElements[i].options[tdElements[i].selectedIndex].value )
			: tdElements[i].options[tdElements[i].selectedIndex].value;
		setSavedUserSettingValue(tdElements[i].id, userSettingValues[userSettingName] );
		saveUserSettingValue(userSettingName, userSettingValues[userSettingName]);
	}
		
}

function getSavedUserSettingValue(userSettingElementsIndex)	{
	return userSettingsDisplayAbleValues[userSettingElementsIndex];
}

function setSavedUserSettingValue(userSettingElementsIndex, newVal)	{
	userSettingsDisplayAbleValues[userSettingElementsIndex] = newVal;
}

function deleteAddressBookEntry( playerId )	{
	playerId = playerId.replace('pic', '');
	
	var trId = 'opt_addr_book_' + playerId;
	var trForDel = $X("//*[@id='mainview']/div/div/table[@id='MessageManagerAddressBookTable']/tbody/tr[@id='" + trId + "']");	
	var rowsAfterThis = $x("//*[@id='mainview']/div/div/table[@id='MessageManagerAddressBookTable']/tbody/tr[@id='" + trId + "']/following-sibling::*");
	
	var i = 0;
	for ( i = 0; i != rowsAfterThis.length; i++ )	{
		var cells = $x("./td", rowsAfterThis[i] );
		var oldId = parseInt( cells[0].id , 0 ) - 1 ;
		cells[0].id =  oldId ;
		if ( oldId % 2 == 0 )	{
			cells[0].style.backgroundColor = '#FAEAC6';
			cells[1].style.backgroundColor = '';
		}
		else	{
			cells[0].style.backgroundColor = '';
			cells[1].style.backgroundColor = '#FAEAC6';
		}
	}
	
	trForDel.parentNode.removeChild( trForDel );
	
	var addressBookList = getAddressBookList();
	delete addressBookList[playerId];
	
	saveAddressBookList( addressBookList );
	
	return;
	
}

function createRowForUserDefinedKeywords(keyName, rowPos)	{
	var res = document.createElement('tr');
	
	if ( rowPos % 2 != 0)	{
		res.style.backgroundColor = '#FAEAC6';
	}
	
	res.id = 'userDefinedKeyword_' + rowPos;
	
	var saveCheckBoxCell = document.createElement('td');
	saveCheckBoxCell.id = 'userDefinedKeyword_saveCheckBoxCell_' + rowPos;	
	var saveCheckBox = createInputElement('checkbox',  'userDefinedKeyword_saveCheckBox_' + rowPos, null, null ); 
	saveCheckBox.style.margin = 'auto';
	saveCheckBox.style.padding = 'auto';		
	saveCheckBoxCell.appendChild( saveCheckBox );	
	res.appendChild( saveCheckBoxCell );	
	
	var keyWordCell = document.createElement('td');
	keyWordCell.id = 'userDefinedKeyword_keyWordCell_' + rowPos;	
	var keyWordCellInput = createInputElement('text',  'userDefinedKeyword_keyWord_' + rowPos, keyName, null );
	keyWordCellInput.maxLength = 10;
	keyWordCellInput.size = 10;	
	keyWordCellInput.style.margin = 'auto';
	keyWordCellInput.style.padding = 'auto';
	keyWordCell.appendChild( keyWordCellInput );
	res.appendChild( keyWordCell );
	
	var menuTextCell = document.createElement('td');
	menuTextCell.id = 'userDefinedKeyword_menuTextCell_' + rowPos;	
	var menuTextCellInput = createInputElement('text',  'userDefinedKeyword_menuText_' + rowPos, keyName != null ? userDefinedMenuOptions[keyName] : null, null );
	menuTextCellInput.style.margin = 'auto';
	menuTextCellInput.style.padding = 'auto';
	menuTextCellInput.maxLength = 20;
	menuTextCellInput.size = 20;	
	menuTextCell.appendChild( menuTextCellInput );
	res.appendChild( menuTextCell );
	
	var iconPathCell = document.createElement('td');
	iconPathCell.id = 'userDefinedKeyword_iconPathCell_' + rowPos;	
	var iconPathInput = createInputElement('text',  'userDefinedKeyword_iconPath_' + rowPos, keyName != null ? userDefinedPicturePaths[keyName] : null , null );
	iconPathInput.style.margin = 'auto';
	iconPathCell.style.padding = 'auto';
	iconPathCell.appendChild( iconPathInput );
	res.appendChild( iconPathCell );
	
	var testButtonCell = document.createElement('td');
	testButtonCell.id = 'userDefinedKeyword_testButtonCell_' + rowPos;
	var testButtonCellButton = createInputElement('button',  'userDefinedKeyword_testButton_' + rowPos, getButtonTextInLang( 'PICTURE_PATH_BUTTON_TEXT' ), 'button' );
	testButtonCellButton.style.margin = 'auto';
	testButtonCellButton.style.padding = 'auto';
	
	addEvent( testButtonCellButton, 'click', function(obj){renderPictureByTest(this)}, true );
	
	testButtonCell.appendChild( testButtonCellButton );
	res.appendChild( testButtonCell );
	
	var testPictureCell = document.createElement('td');
	testPictureCell.id = 'userDefinedKeyword_testPictureCell_' + rowPos;	
	testPictureCell.style.margin = 'auto';
	testPictureCell.style.padding = 'auto';	
	if ( keyName != null )	{
		testPictureCell.appendChild( createSubjectPicture( userDefinedPicturePaths[keyName] ) );
	}
	res.appendChild( testPictureCell );
	
	return res;
}

function renderPictureByTest(pushedButton)	{
	var actualRow = pushedButton.parentNode.parentNode;
	var pictureCell = $X("./td[starts-with(@id,'userDefinedKeyword_testPictureCell')]", actualRow );
	var picturePathInput = $X("./td[starts-with(@id,'userDefinedKeyword_iconPath')]/input", actualRow );
	var picturePath = picturePathInput.value;
	//	/skin/characters/fleet/40x40/ship_transport_r_40x40.gif
	pictureCell.appendChild( createSubjectPicture( picturePath ) );
}

function addNewUserDefinedKeywordLine()	{	
	var userDefinedKeywordsTable = $X("//*[@id='mainview']/div/div/table[@id='UserDefinedKeywordTable']/tbody");
	var previousLineCount = $x("./tr", userDefinedKeywordsTable).length;
	userDefinedKeywordsTable.appendChild( createRowForUserDefinedKeywords(null, previousLineCount + 1) );
}

function saveUserDefinedKeywords()	{
	var userDefinedKeywordsTable = $X("//*[@id='mainview']/div/div/table[@id='UserDefinedKeywordTable']");
	var allSaveCheckboxes = $x("./tbody/tr/td/input[starts-with(@id,'userDefinedKeyword_saveCheckBox')]", userDefinedKeywordsTable);
	var i = 0;
	var found = false;
	if ( userDefinedMenuOptions == null )	{
		userDefinedMenuOptions = new Object();
		userDefinedPicturePaths = new Object();
	}
	for ( i = 0; i != allSaveCheckboxes.length; i++ )	{
		if ( allSaveCheckboxes[i].checked )	{
			found = true;
			var parentRow = allSaveCheckboxes[i].parentNode.parentNode;
			var keyWordInput = $X("./td/input[starts-with(@id,'userDefinedKeyword_keyWord')]", parentRow);
			var menuInput = $X("./td/input[starts-with(@id,'userDefinedKeyword_menuText')]", parentRow);
			var pathInput = $X("./td/input[starts-with(@id,'userDefinedKeyword_iconPath')]", parentRow);
			if ( keyWordInput.value == null || menuInput.value == null || pathInput.value == null )	{
				continue;
			}
			userDefinedMenuOptions[keyWordInput.value] = menuInput.value;
			userDefinedPicturePaths[keyWordInput.value] = pathInput.value;	
		}
	}
	if ( found )	{
		GM_log( 'start save ');
		saveUserDefinedKeywordsToFF( userDefinedMenuOptions, userDefinedPicturePaths );
		window.location.reload( false );
	}
}

function deleteUserDefinedKeyWords()	{
	var userDefinedKeywordsTable = $X("//*[@id='mainview']/div/div/table[@id='UserDefinedKeywordTable']");
	var allSaveCheckboxes = $x("./tbody/tr/td/input[starts-with(@id,'userDefinedKeyword_saveCheckBox')]", userDefinedKeywordsTable);
	var i = 0;
	if ( userDefinedMenuOptions == null )	{
		// nothing from remove
		return;
	}
	var found = false;
	for ( i = 0; i != allSaveCheckboxes.length; i++ )	{
		if ( allSaveCheckboxes[i].checked )	{
			found = true;
			var parentRow = allSaveCheckboxes[i].parentNode.parentNode;
			var keyWordInput = $X("./td/input[starts-with(@id,'userDefinedKeyword_keyWord')]", parentRow);
			if ( keyWordInput.value == null )	{
				continue;
			}
			delete userDefinedMenuOptions[keyWordInput.value];
			delete userDefinedPicturePaths[keyWordInput.value];						
		}
	}
	if ( found )	{
		GM_log( 'start delete ');
		saveUserDefinedKeywordsToFF( userDefinedMenuOptions, userDefinedPicturePaths );
		window.location.reload( false );
	}
}

function exportUserDefinedKeywords()	{
	var userDefinedKeyword = getUserSettingValue( 'USER_DEFINED_KEYWORDS', '' );
	if ( userDefinedKeyword == 'undefined' || userDefinedKeyword == null || userDefinedKeyword.length == 0 )	{
		return false;
	}
	
	var menuOptionsPathMap = new Object();
	var iconPathMap = new Object();
	
	var res = '';
	var usedServer = '';
	var i = 0;
	var userDefinedKeywordVector = userDefinedKeyword.split('###');
	var pairedData = null;
	var j = 0;
	var keyWord = null;
	var menuOption = null;
	var iconPath = null;
	for ( i in userDefinedKeywordVector )	{				
		usedServer = userDefinedKeywordVector[i].split('##')[0];
		if ( usedServer == gameServer )	{
			prompt(getOtherTextInLang( 'EXPORT_USER_DEFINED_KEYWORDS_MESSAGE' ),  userDefinedKeywordVector[i].split('##')[1] );
			continue;
		}
	}
	return true;
}

//	{keyword00=option00;/favicon.ico||keyword01=option01;picture01path||1=2;3}
//	{keyword00=option00;/favicon.ico}
//	[\w||\/||\.-_]+=[\w||\/||\.-_]+;[\w||\/||\.-_]+
//function importUserDefinedKeywords(override)	{
function importUserDefinedKeywords()	{
	var newKeywords = prompt(getOtherTextInLang( 'IMPORT_USER_DEFINED_KEYWORDS_MESSAGE' ),  '' );
	if ( newKeywords == null || newKeywords.length == 0 )	{
		return false;
	}
//	if ( !newKeywords.match(/\{[A-Z0-9_\/\.-]+=[A-Z0-9_\/\.-]+;[A-Z0-9_\/\.-]+\}/) )	{
	if ( newKeywords.match(/^\{/) && newKeywords.match(/\}$/) ) {
		var onlyKeywords = newKeywords.replace(/\{/, '').replace(/\}/, '');
		if ( onlyKeywords.match(/[A-Z0-9_\/\.-]+=/)  )	{
			alert ( 'HIT' );
		}
	}
	
	return true;
}

function setupUserDefinedKeywordMenus() {
	GM_registerMenuCommand("Message Manager: " + getOtherTextInLang( 'EXPORT_USER_DEFINED_KEYWORDS' ), exportUserDefinedKeywords);
	GM_registerMenuCommand("Message Manager: " + getOtherTextInLang( 'IMPORT_USER_DEFINED_KEYWORDS' ), importUserDefinedKeywords);
}